﻿using System;
using System.Web.Mvc;
using MvcContrib;
using ScrumBox.Model;
using ScrumBox.Web.Helpers;

namespace ScrumBox.Web.Controllers
{
    public class SprintController : Controller
    {
        private readonly IProjectRepository _projectRepository;
        private readonly ISprintRepository _sprintRepository;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="projectRepository"></param>
        /// <param name="sprintRepository"></param>
        public SprintController(IProjectRepository projectRepository, ISprintRepository sprintRepository)
        {
            _projectRepository = projectRepository;
            _sprintRepository = sprintRepository;

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="projectKey"></param>
        /// <returns></returns>
        public ActionResult List(string projectKey)
        {
            var sprints = _projectRepository.GetByKey(projectKey).GetSprints();
            ViewData.Add(sprints);
            return View();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sprintKey"></param>
        /// <returns></returns>
        public ActionResult Details(string sprintKey)
        {
            ViewData.Add(_sprintRepository.GetByKey(sprintKey));
            return View();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="projectKey"></param>
        /// <returns></returns>
        public ActionResult New(string projectKey)
        {
            var sprint = new Sprint (_projectRepository.GetByKey(projectKey));
            ViewData.Add(sprint);
            return View();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="projectKey"></param>
        /// <param name="title"></param>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <returns></returns>
        public ActionResult Save(string projectKey, string title, DateTime startDate, DateTime endDate)
        {
            var project = _projectRepository.GetByKey(projectKey);
            var sprintKey = string.Format("{0}-{1}", projectKey, title);
            project.AddSprint(new Sprint(sprintKey, title, startDate, endDate));
            _projectRepository.Save(project);
            TempData[TempDataKeys.Message] = "Sprint saved";
            return RedirectToAction("details", "project", new { key = projectKey });
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public ActionResult AddSprintBackLogItem(string sprintKey, string projectKey, string title)
        {
            var sprint = _sprintRepository.GetByKey(sprintKey);
            var sprintBackLogItem = new SprintBackLogItem {Title = title};
            sprint.AddBackLogItem(sprintBackLogItem);
            _sprintRepository.Save(sprint);
            TempData[TempDataKeys.Message] = "The sprint was created successfully.";
            return RedirectToAction("Details");
        }
    }
}